﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aulac.Client.Data.XmlQuery;
using Aulac.Rsc.Bss.Tags;
using Aulac.Verb;
using System.Xml;
using Aulac.Global.Xml;
using System.Data;
using Aulac.Client.Global.Interface;
using Aulac.Global.Common;
using Aulac.Global.Enums;
using Aulac.Rsc.Bss;

namespace Aulac.Client.Bss.WinForm.CapAss
{
    public partial class FormDlgCapAss
    {
        /// <summary>
        /// GET thông tin chi tiết của Tài sản.
        /// </summary>
        private bool GetDetailItemCapAss()
        {
            ClientExecXmlQueryData query = new ClientExecXmlQueryData();
            query.SetRequestInfo(Aulac.Verb.AulacVerb.DataQuery.QueryData, Aulac.Verb.AulacVerb.DataQuery.Noun.AulacDataQuery);
            query.SetVchHeaderElm();
            string sqlQuery = string.Format("SELECT {0}", "*");
            sqlQuery += string.Format(" FROM {0}", TableNames.Dm_CapAss);             
            sqlQuery += string.Format(" WHERE {0} = @{0} ", tagItems.CapAss.CapAssPrkID);
            query.ObjQueryVchHeader.SqlQueryString = sqlQuery;
            query.ObjQueryVchHeader.AddValueParamFilterItem(tagItems.CapAss.CapAssPrkID, tagItems.CapAss.CapAssPrkID, CapAssPrkID);

            try
            {
                XmlDataDocument xmlDoc = query.ExecureQueryData();
                XmlElement eHeader = XmlProcess.GetVchHeaderElm(xmlDoc);
                List<IAulacDataControl> mListControls = null;
                GetListAulacDataControls(ref mListControls, grBoxInfo);
                GetListAulacDataControls(ref mListControls, groupBox1);
                //string stmp = XmlProcess.GetValueVchHeader(xmlDoc, tagItems.CapAss.CapAssTypePrkID);
             
                foreach (IAulacValueDataCtrl ctr in mListControls)
                {
                    ctr.SetValueToControl(eHeader);
                }
                return true;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Save Item CapAss.
        /// </summary>
        private bool SaveItemCapAss(ref XmlDataDocument xmlData)
        {
            ClientExecXmlQueryDataCatalog query = new ClientExecXmlQueryDataCatalog();
            query.SetRequestInfo(AulacVerb.DataQuery.QueryData, AulacVerb.DataQuery.Noun.AulacDataQuery);
            query.ObjQueryVchLines.QueryCmdType = (FormMode == FormModes.NewMode) ? QueryCmdTypes.AddDataTable : QueryCmdTypes.UpdateDataTable;

            query.ObjQueryVchLines.SetTableName(TableNames.Dm_CapAss);            
            
            query.ObjQueryVchLines.AddFieldCheckValidItem(tagItems.CapAss.CapAssID, txtCapAssID.Text);
            query.ObjQueryVchLines.AddFieldCheckValidItem(tagItems.CapAss.CapAssName, txtCapAssName.Text);
            query.ObjQueryVchLines.AddFieldPrk(tagItems.CapAss.CapAssPrkID, DbType.Decimal, KeyTypes.AutoNumber);
            if (FormMode == FormModes.EditMode)
                query.ObjQueryVchLines.AddValueParamFilterItem(tagItems.CapAss.CapAssPrkID, tagItems.CapAss.CapAssPrkID, CapAssPrkID);
            List<IAulacDataControl> mListControl = null;
            GetListAulacDataControls(ref mListControl, groupBox1);
            GetListAulacDataControls(ref mListControl, grBoxInfo);
            XmlElement eLine = query.ObjQueryVchLines.AddVchLineItemElm();
            foreach (IAulacValueDataCtrl ctr in mListControl)
                ctr.GetValueToXml(ref eLine);

            try
            {
                XmlDataDocument xmlDoc = query.ExecureQueryData();
                xmlData = xmlDoc;
                CapAssPrkID = XmlProcess.GetValueVchLineItemFirst(xmlDoc, tagItems.CapAss.CapAssPrkID);
                return true;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Delete mot Item CapAss.
        /// </summary>
        public bool DeleteCapAss()
        {
            ClientExecXmlQueryData query = new ClientExecXmlQueryData();
            query.SetRequestInfo(Verb.AulacVerb.DataQuery.QueryData, Verb.AulacVerb.DataQuery.Noun.AulacDataQuery);
            query.SetVchHeaderElm();
            string strQuery = string.Format("DELETE FROM {0} ", TableNames.Dm_CapAss);
            strQuery += string.Format(" WHERE {0} = @{0} ", tagItems.CapAss.CapAssPrkID);
            query.ObjQueryVchHeader.SqlQueryString = strQuery;
            query.ObjQueryVchHeader.AddValueParamFilterItem(tagItems.CapAss.CapAssPrkID, tagItems.CapAss.CapAssPrkID, CapAssPrkID);

            try
            {
                query.ExecureNoneQuery();
                return true;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
    }
}
